package com.game.center.service;

import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.game.center.messages.request.*;
import com.game.center.messages.response.ActivityFishResponseVo;
import com.game.center.messages.response.ActivityInfoResponseVo;
import com.game.center.messages.response.ActivityListResponseVo;
import com.game.center.messages.response.FishPoleVo;
import com.game.center.messages.response.vo.*;
import com.game.framework.dao.entity.*;
import com.game.framework.dao.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    protected ActivityFishBaitMapper activityFishBaitMapper;
    @Autowired
    protected ActivityFishPoleMapper activityFishPoleMapper;
    @Autowired
    protected ActivityFishTypeMapper activityFishTypeMapper;
    @Autowired
    protected ActivityObjectMapper activityObjectMapper;
    @Autowired
    protected ActivityTicketMapper activityTicketMapper;
    @Autowired
    protected ActivityPlayerMapper activityPlayerMapper;
    @Autowired
    protected ActivityRankMapper activityRankMapper;
    @Autowired
    protected ActivityMapper activityMapper;
    @Autowired
    protected FishPoleMapper fishPoleMapper;
    @Autowired
    protected AwardMapper awardMapper;
    @Autowired
    protected ActivityAwardMapper activityAwardMapper;
    @Override
    public List<ActivityListResponseVo> getActivityList(ActivityListRequestParam requestParam) {
        return getActivityList();
    }

    @Override
    public ActivityInfoResponseVo getActivityInfo(ActivityInfoRequestParam requestParam) {
        ActivityInfoResponseVo activityInfoResponseVo = new ActivityInfoResponseVo();
        ActivityEntity activityEntity = activityMapper.selectById(requestParam.getId());
        if(ObjectUtils.isNotEmpty(activityEntity)){
            activityInfoResponseVo.setId(activityEntity.getId());
            activityInfoResponseVo.setName(activityEntity.getName());
            activityInfoResponseVo.setLimitPlayer(activityEntity.getLimitPlayer());
            //activityInfoResponseVo.setLimitDragonType(activityEntity.getLimitDragonType());
            //activityInfoResponseVo.setLimitDragonNum(activityEntity.getLimitDragonNum());
            activityInfoResponseVo.setStartTime(activityEntity.getStartTime());
            activityInfoResponseVo.setEndTime(activityEntity.getEndTime());
            activityInfoResponseVo.setStatus(activityEntity.getStatus());
            activityInfoResponseVo.setRank(activityEntity.getRank());

            int ticketCount = activityTicketMapper.selectCountByActivityId(activityEntity.getId());
            activityInfoResponseVo.setTicketCount(ticketCount);
            int fishBaitCount = activityFishBaitMapper.selectCountByActivityId(activityEntity.getId());
            activityInfoResponseVo.setFishBaitCount(fishBaitCount);
            int playerCount = activityPlayerMapper.selectCountByActivityId(activityEntity.getId());
            activityInfoResponseVo.setPlayerCount(playerCount);
            List<ActivityFishTypeEntity>  activityFishTypeEntityList = activityFishTypeMapper.selectListByActivityId(activityEntity.getId());
            if(CollectionUtils.isNotEmpty(activityFishTypeEntityList)) {
                List<ActivityFishTypeVo> ActivityFishTypeVos = new ArrayList<>();
                BeanUtils.copyProperties(activityFishTypeEntityList, ActivityFishTypeVos);
                activityInfoResponseVo.setFishTypeVos(ActivityFishTypeVos);
            }
            List<ActivityRankVo> activityRankVos = new ArrayList<>();
            List<ActivityRankEntity> activityRankEntityList = activityRankMapper.selectListByActivityId(activityEntity.getId());
            if(CollectionUtils.isNotEmpty(activityRankEntityList)) {
                BeanUtils.copyProperties(activityRankEntityList, activityRankVos);
            }
            activityInfoResponseVo.setPlayerRankVos(activityRankVos);
        }
        return activityInfoResponseVo;
    }

    @Override
    public List<ActivityFishResponseVo> getActivityPlayerFishList(ActivityFishRequestParam requestParam) {
        List<ActivityFishResponseVo> activityFishResponseVos = new ArrayList<>();
        Map<String, Object>sqlMap = new HashMap<>();
        sqlMap.put("activityId",requestParam.getId());
        sqlMap.put("userId",requestParam.getUserId());
        sqlMap.put("roleId",requestParam.getRoleId());
        List<ActivityPlayerEntity> activityPlayerEntityList = activityPlayerMapper.selectByMap(sqlMap);
        if(CollectionUtils.isNotEmpty(activityPlayerEntityList)){
            BeanUtils.copyProperties(activityPlayerEntityList, activityFishResponseVos);
        }
        return activityFishResponseVos;
    }

    @Override
    public List<ActivityRankVo> getActivityPlayerRankInfo(ActivityRankRequestParam requestParam) {
        Map<String, Object>sqlMap = new HashMap<>();
        sqlMap.put("activityId",requestParam.getId());
        sqlMap.put("userId",requestParam.getUserId());
        sqlMap.put("roleId",requestParam.getRoleId());
        sqlMap.put("phone",requestParam.getPhone());
        List<ActivityRankVo> activityRankVos = new ArrayList<>();
        List<ActivityRankEntity> activityRankEntityList = activityRankMapper.selectByMap(sqlMap);
        if(CollectionUtils.isNotEmpty(activityRankEntityList)) {
            BeanUtils.copyProperties(activityRankEntityList, activityRankVos);
        }
        return activityRankVos;
    }

    public List<ActivityListResponseVo> getActivityList() {
        List<ActivityListResponseVo> activityListResponseVos = new ArrayList<>();
        List<ActivityEntity> activityEntityList = activityMapper.selectAll();
        if(CollectionUtils.isNotEmpty(activityEntityList)){
            for(ActivityEntity activityEntity : activityEntityList){
                ActivityListResponseVo activityListResponseVo = new ActivityListResponseVo();
                BeanUtils.copyProperties(activityEntity, activityListResponseVo);
                List<ActivityFishTypeEntity>  activityFishTypeEntityList = activityFishTypeMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityFishTypeEntityList)) {
                    List<ActivityFishTypeVo> ActivityFishTypeVos = new ArrayList<>();
                    BeanUtils.copyProperties(activityFishTypeEntityList, ActivityFishTypeVos);
                    activityListResponseVo.setFishTypeVos(ActivityFishTypeVos);
                }

                List<ActivityObjectEntity>  activityObjectEntityList = activityObjectMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityObjectEntityList)) {
                    List<ActivityObjectVo> ActivityObjectVos = new ArrayList<>();
                    BeanUtils.copyProperties(activityObjectEntityList, ActivityObjectVos);
                    activityListResponseVo.setObjectVos(ActivityObjectVos);
                }
                List<ActivityFishBaitEntity>  activityFishBaitEntityList = activityFishBaitMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityFishBaitEntityList)) {
                    List<ActivityFishBaitVo> ActivityFishBaitVos = new ArrayList<>();
                    BeanUtils.copyProperties(activityFishBaitEntityList, ActivityFishBaitVos);
                    activityListResponseVo.setFishBaitVos(ActivityFishBaitVos);
                }
                List<ActivityTicketEntity>  activityTicketEntityList = activityTicketMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityTicketEntityList)) {
                    List<ActivityTicketVo> ActivityTicketVos = new ArrayList<>();
                    BeanUtils.copyProperties(activityTicketEntityList, ActivityTicketVos);
                    activityListResponseVo.setTicketVos(ActivityTicketVos);
                }
                List<ActivityFishPoleEntity>  activityFishPoleEntityList = activityFishPoleMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityFishPoleEntityList)) {
                    List<ActivityFishPoleVo> ActivityFishPoleVos = new ArrayList<>();
                    BeanUtils.copyProperties(activityFishPoleEntityList, ActivityFishPoleVos);
                    activityListResponseVo.setFishPoleVos(ActivityFishPoleVos);
                }
                activityListResponseVos.add(activityListResponseVo);
            }
        }
        return activityListResponseVos;
    }
    @Override
    public List<ActivityListResponseVo> addActivityInfo(ActivityAddRequestParam requestParam) {
        ActivityEntity activityEntity = new ActivityEntity();
        activityEntity.setName(requestParam.getName());
        activityEntity.setLimitPlayer(requestParam.getLimitPlayer());
        //activityEntity.setLimitDragonType(requestParam.getLimitDragonType());
        //activityEntity.setLimitDragonNum(requestParam.getLimitDragonNum());
        activityEntity.setStartTime(requestParam.getStartTime());
        activityEntity.setEndTime(requestParam.getEndTime());
        activityEntity.setRank(requestParam.getRank());
        activityEntity.setStatus(0);
        Integer activityId = activityMapper.insert(activityEntity);
        if(CollectionUtils.isNotEmpty(requestParam.getFishTypeVos())){
            for(ActivityFishTypeVo activityFishTypeVo : requestParam.getFishTypeVos()){
                ActivityFishTypeEntity activityFishTypeEntity = new ActivityFishTypeEntity();
                activityFishTypeEntity.setActivityId(activityId.longValue());
                activityFishTypeEntity.setFishType(activityFishTypeVo.getFishType());
                activityFishTypeEntity.setFishName(activityFishTypeVo.getFishName());
                activityFishTypeEntity.setFishCount(activityFishTypeVo.getFishCount());
                activityFishTypeMapper.insert(activityFishTypeEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getObjectVos())){
            for(ActivityObjectVo activityObjectVo : requestParam.getObjectVos()){
                ActivityObjectEntity activityObjectEntity = new ActivityObjectEntity();
                activityObjectEntity.setActivityId(activityId.longValue());
                activityObjectEntity.setObjectId(activityObjectVo.getObjectId());
                activityObjectEntity.setObjectName(activityObjectVo.getObjectName());
                activityObjectEntity.setNumber(activityObjectVo.getNumber());
                activityObjectMapper.insert(activityObjectEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getFishBaitVos())){
            for(ActivityFishBaitVo activityFishBaitVo : requestParam.getFishBaitVos()){
                ActivityFishBaitEntity activityFishBaitEntity = new ActivityFishBaitEntity();
                activityFishBaitEntity.setActivityId(activityId.longValue());
                activityFishBaitEntity.setBaitType(activityFishBaitVo.getBaitType());
                activityFishBaitEntity.setNumber(activityFishBaitVo.getNumber());
                activityFishBaitMapper.insert(activityFishBaitEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getTicketVos())){
            for(ActivityTicketVo activityTicketVo : requestParam.getTicketVos()){
                ActivityTicketEntity activityTicketEntity = new ActivityTicketEntity();
                activityTicketEntity.setActivityId(activityId.longValue());
                activityTicketEntity.setTicketType(activityTicketVo.getTicketType());
                activityTicketEntity.setNumber(activityTicketVo.getNumber());
                activityTicketMapper.insert(activityTicketEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getFishPoleVos())){
            for(ActivityFishPoleVo activityFishPoleVo : requestParam.getFishPoleVos()){
                ActivityFishPoleEntity activityFishPoleEntity = new ActivityFishPoleEntity();
                activityFishPoleEntity.setActivityId(activityId.longValue());
                activityFishPoleEntity.setFishType(activityFishPoleVo.getFishType());
                activityFishPoleEntity.setPoleId(activityFishPoleVo.getPoleId());
                activityFishPoleEntity.setNumber(activityFishPoleVo.getNumber());
                activityFishPoleMapper.insert(activityFishPoleEntity);
            }
        }
        return getActivityList();
    }

    @Override
    public List<ActivityListResponseVo> editActivityInfo(ActivityEditRequestParam requestParam) {
        ActivityEntity activityEntity = activityMapper.selectById(requestParam.getId());
        if(ObjectUtils.isEmpty(activityEntity)){
            return getActivityList();
        }
        activityEntity.setName(requestParam.getName());
        activityEntity.setLimitPlayer(requestParam.getLimitPlayer());
        //activityEntity.setLimitDragonType(requestParam.getLimitDragonType());
        //activityEntity.setLimitDragonNum(requestParam.getLimitDragonNum());
        activityEntity.setStartTime(requestParam.getStartTime());
        activityEntity.setEndTime(requestParam.getEndTime());
        activityEntity.setRank(requestParam.getRank());
        activityEntity.setStatus(0);
        activityMapper.updateById(activityEntity);
        Long activityId = requestParam.getId();
        activityFishTypeMapper.deleteAllByActivityId(activityId);
        activityObjectMapper.deleteAllByActivityId(activityId);
        activityFishBaitMapper.deleteAllByActivityId(activityId);
        activityTicketMapper.deleteAllByActivityId(activityId);
        activityFishPoleMapper.deleteAllByActivityId(activityId);
        if(CollectionUtils.isNotEmpty(requestParam.getFishTypeVos())){
            for(ActivityFishTypeVo activityFishTypeVo : requestParam.getFishTypeVos()){
                ActivityFishTypeEntity activityFishTypeEntity = new ActivityFishTypeEntity();
                activityFishTypeEntity.setActivityId(activityId);
                activityFishTypeEntity.setFishType(activityFishTypeVo.getFishType());
                activityFishTypeEntity.setFishName(activityFishTypeVo.getFishName());
                activityFishTypeEntity.setFishCount(activityFishTypeVo.getFishCount());
                activityFishTypeMapper.insert(activityFishTypeEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getObjectVos())){
            for(ActivityObjectVo activityObjectVo : requestParam.getObjectVos()){
                ActivityObjectEntity activityObjectEntity = new ActivityObjectEntity();
                activityObjectEntity.setActivityId(activityId);
                activityObjectEntity.setObjectId(activityObjectVo.getObjectId());
                activityObjectEntity.setObjectName(activityObjectVo.getObjectName());
                activityObjectEntity.setNumber(activityObjectVo.getNumber());
                activityObjectMapper.insert(activityObjectEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getFishBaitVos())){
            for(ActivityFishBaitVo activityFishBaitVo : requestParam.getFishBaitVos()){
                ActivityFishBaitEntity activityFishBaitEntity = new ActivityFishBaitEntity();
                activityFishBaitEntity.setActivityId(activityId);
                activityFishBaitEntity.setBaitType(activityFishBaitVo.getBaitType());
                activityFishBaitEntity.setNumber(activityFishBaitVo.getNumber());
                activityFishBaitMapper.insert(activityFishBaitEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getTicketVos())){
            for(ActivityTicketVo activityTicketVo : requestParam.getTicketVos()){
                ActivityTicketEntity activityTicketEntity = new ActivityTicketEntity();
                activityTicketEntity.setActivityId(activityId);
                activityTicketEntity.setTicketType(activityTicketVo.getTicketType());
                activityTicketEntity.setNumber(activityTicketVo.getNumber());
                activityTicketMapper.insert(activityTicketEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(requestParam.getFishPoleVos())){
            for(ActivityFishPoleVo activityFishPoleVo : requestParam.getFishPoleVos()){
                ActivityFishPoleEntity activityFishPoleEntity = new ActivityFishPoleEntity();
                activityFishPoleEntity.setActivityId(activityId);
                activityFishPoleEntity.setFishType(activityFishPoleVo.getFishType());
                activityFishPoleEntity.setPoleId(activityFishPoleVo.getPoleId());
                activityFishPoleEntity.setNumber(activityFishPoleVo.getNumber());
                activityFishPoleMapper.insert(activityFishPoleEntity);
            }
        }
        return getActivityList();
    }

    @Override
    public List<ActivityListResponseVo> deleteActivityInfo(ActivityDeleteRequestParam requestParam) {
        Long activityId = requestParam.getId();
        activityMapper.deleteById(activityId);
        activityFishTypeMapper.deleteAllByActivityId(activityId);
        activityObjectMapper.deleteAllByActivityId(activityId);
        activityFishBaitMapper.deleteAllByActivityId(activityId);
        activityTicketMapper.deleteAllByActivityId(activityId);
        activityFishPoleMapper.deleteAllByActivityId(activityId);
        activityPlayerMapper.deleteAllByActivityId(activityId);
        return getActivityList();
    }

    @Override
    public List<FishPoleVo> getFishPoleList(FishPoleListRequestParam requestParam) {
        List<FishPoleVo> fishPoleVos = new ArrayList<>();
        List<FishPoleEntity> fishPoleEntities = fishPoleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(fishPoleEntities)){
            BeanUtils.copyProperties(fishPoleEntities,fishPoleVos);
        }
        return fishPoleVos;
    }

    @Override
    public List<FishPoleVo> addFishPole(FishPoleAddRequestParam requestParam) {
        FishPoleEntity fishPoleEntity = new FishPoleEntity();
        fishPoleEntity.setName(requestParam.getName());
        fishPoleEntity.setFishType(requestParam.getFishType());
        fishPoleEntity.setMinRate(requestParam.getMinRate());
        fishPoleEntity.setMaxRate(requestParam.getMaxRate());
        fishPoleMapper.insert(fishPoleEntity);
        List<FishPoleVo> fishPoleVos = new ArrayList<>();
        List<FishPoleEntity> fishPoleEntities = fishPoleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(fishPoleEntities)){
            BeanUtils.copyProperties(fishPoleEntities,fishPoleVos);
        }
        return fishPoleVos;
    }

    @Override
    public List<FishPoleVo> editFishPole(FishPoleEditRequestParam requestParam) {
        FishPoleEntity fishPoleEntity = fishPoleMapper.selectById(requestParam.getId());
        if(ObjectUtils.isNotEmpty(fishPoleEntity)) {
            fishPoleEntity.setName(requestParam.getName());
            fishPoleEntity.setFishType(requestParam.getFishType());
            fishPoleEntity.setMinRate(requestParam.getMinRate());
            fishPoleEntity.setMaxRate(requestParam.getMaxRate());
            fishPoleMapper.updateById(fishPoleEntity);
        }
        List<FishPoleVo> fishPoleVos = new ArrayList<>();
        List<FishPoleEntity> fishPoleEntities = fishPoleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(fishPoleEntities)){
            BeanUtils.copyProperties(fishPoleEntities,fishPoleVos);
        }
        return fishPoleVos;
    }

    @Override
    public List<FishPoleVo> deleteFishPole(FishPoleDeleteRequestParam requestParam) {
        int count = fishPoleMapper.deleteById(requestParam.getId());
        if(count > 0) {
            log.info("deleteFishPole id={} success!",requestParam.getId());
        }
        List<FishPoleVo> fishPoleVos = new ArrayList<>();
        List<FishPoleEntity> fishPoleEntities = fishPoleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(fishPoleEntities)){
            BeanUtils.copyProperties(fishPoleEntities,fishPoleVos);
        }
        return fishPoleVos;
    }

    @Override
    public List<AwardVo> getAwardList(AwardListRequestParam requestParam) {
        List<AwardVo> awardVos = new ArrayList<>();
        List<AwardEntity> awardEntities = awardMapper.selectAll();
        if(CollectionUtils.isNotEmpty(awardEntities)){
            BeanUtils.copyProperties(awardEntities,awardVos);
        }
        return awardVos;
    }

    @Override
    public List<AwardVo> addAwardInfo(AwardAddRequestParam requestParam) {
        AwardEntity awardEntity = new AwardEntity();
        awardEntity.setAwardType(requestParam.getAwardType());
        awardEntity.setAwardName(requestParam.getAwardName());
        awardEntity.setObjectId(requestParam.getObjectId());
        awardEntity.setFishType(requestParam.getFishType());
        awardEntity.setFishCount(requestParam.getFishCount());
        awardEntity.setAllCount(requestParam.getAllCount());
        awardEntity.setRemainCount(requestParam.getRemainCount());
        awardEntity.setImg(requestParam.getImg());
        awardEntity.setUpdown(requestParam.getUpdown());
        awardMapper.insert(awardEntity);
        List<AwardVo> awardVos = new ArrayList<>();
        List<AwardEntity> awardEntities = awardMapper.selectAll();
        if(CollectionUtils.isNotEmpty(awardEntities)){
            BeanUtils.copyProperties(awardEntities,awardVos);
        }
        return awardVos;
    }

    @Override
    public List<AwardVo> editAwardInfo(AwardEditRequestParam requestParam) {
        AwardEntity awardEntity = awardMapper.selectById(requestParam.getId());
        if(ObjectUtils.isNotEmpty(awardEntity)) {
            awardEntity.setAwardType(requestParam.getAwardType());
            awardEntity.setAwardName(requestParam.getAwardName());
            awardEntity.setObjectId(requestParam.getObjectId());
            awardEntity.setFishType(requestParam.getFishType());
            awardEntity.setFishCount(requestParam.getFishCount());
            awardEntity.setAllCount(requestParam.getAllCount());
            awardEntity.setRemainCount(requestParam.getRemainCount());
            awardEntity.setImg(requestParam.getImg());
            awardEntity.setUpdown(requestParam.getUpdown());
            awardMapper.updateById(awardEntity);
        }
        List<AwardVo> awardVos = new ArrayList<>();
        List<AwardEntity> awardEntities = awardMapper.selectAll();
        if(CollectionUtils.isNotEmpty(awardEntities)){
            BeanUtils.copyProperties(awardEntities,awardVos);
        }
        return awardVos;
    }

    @Override
    public List<AwardVo> deleteAwardInfo(AwardDeleteRequestParam requestParam) {
        int count = awardMapper.deleteById(requestParam.getId());
        if(count > 0) {
            log.info("deleteAwardInfo id={} success!",requestParam.getId());
        }
        List<AwardVo> awardVos = new ArrayList<>();
        List<AwardEntity> awardEntities = awardMapper.selectAll();
        if(CollectionUtils.isNotEmpty(awardEntities)){
            BeanUtils.copyProperties(awardEntities,awardVos);
        }
        return awardVos;
    }

    @Override
    public List<ActivityAwardVo> getActivityAwardList(ActivityAwardRequestParam requestParam) {
        Map<String, Object>sqlMap = new HashMap<>();
        sqlMap.put("activityId",requestParam.getId());
        sqlMap.put("phone",requestParam.getPhone());
        sqlMap.put("fishType",requestParam.getFishType());
        sqlMap.put("awardType",requestParam.getAwardType());
        List<ActivityAwardVo> activityAwardVos = new ArrayList<>();
        List<ActivityAwardEntity> activityAwardEntityList = activityAwardMapper.selectByMap(sqlMap);
        if(CollectionUtils.isNotEmpty(activityAwardEntityList)) {
            BeanUtils.copyProperties(activityAwardEntityList, activityAwardVos);
        }
        return activityAwardVos;
    }

    @Override
    public void test(){

    }
}
